// pages/withdrawMoney/withdrawMoney.js
import userApi from '../../service/userApi'
import { checkPhone } from '../../utils/util'
import { $wuxLoading } from '../../components/wux/index'

const app = getApp()
let codeConfig = app.globalData.codeConfig
Page({

  /**
   * 页面的初始数据
   */
  data: {
    amount: '', // 提现金额
    totalAmount: 0, // 最多能提现金额
    disabled: true,
    showErrorText: false,
    errText: '',
    formData: {
      telephone: '',
      amount: ''
    },
    userInfo: {},
    moneyInputPlaceholder: true, // 金额placeholder
    withdrawMoneySuccess: false, // 提现成功
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.setData({
      userInfo: app.globalData.userInfo
    })
    that.$wuxLoading = $wuxLoading()
    if (options.actualEarnings) {
      that.setData({
        totalAmount: options.actualEarnings - 0
      })
    }
  },
  getTelephone (e) {
    this.setData({
      'formData.telephone': e.detail.value
    })
    this.canClick()
  },
  getAmount (e) {
    var _val = e.detail.value - 0
    var maxAmount = this.data.totalAmount > 5000 ? 5000 : this.data.totalAmount
    this.setData({
      'formData.amount': e.detail.value.trim()
    })
    if (e.detail.value.trim() != '') {
      this.setData({
        moneyInputPlaceholder: false,
      })
      if (e.detail.value != '' && _val > maxAmount) {
        this.setData({
          showErrorText: true
        })
        if (this.data.totalAmount > 5000) {
          this.setData({
            errText: '单笔最多提现5000元'
          })
        } else {
          this.setData({
            errText: '超过可提现金额'
          })
        }
      } else if (e.detail.value != '' && _val < 1) {
        this.setData({
          showErrorText: true,
          errText: '单笔最少提现1元'
        })
      } else {
        this.setData({
          showErrorText: false
        })
      }
      this.canClick()
    } else {
      this.setData({
        moneyInputPlaceholder: true,
        showErrorText: false,
        disabled: true
      })
    }
  },
  // 提现按钮活性
  canClick () {
    var that = this
    var telephone = that.data.formData.telephone
    var amount = that.data.formData.amount.trim() - 0
    var maxAmount = this.data.totalAmount > 5000 ? 5000 : this.data.totalAmount
    if (telephone != '' && amount <= maxAmount && amount >= 1) {
      that.setData({
        disabled: false
      })
    } else {
      that.setData({
        disabled: true
      })
    }
  },
  // 提现
  formSubmit () {
    let that = this
    var params = {...this.data.formData}
    params.amount = (params.amount - 0).toFixed(2) + ''
    // 校验表单
    if (!checkPhone(params.telephone)){
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none'
      })
      return false
    }
    userApi.getWithdraw(params).then(({data}) => {
      that.$wuxLoading.show({ text: '加载中...' })
      wx.setStorage({
        key: "moneyTelephone",
        data: params.telephone
      })
      if (data.errCode == codeConfig.errCode.SUCCESS_CODE) {
        that.setData({
          withdrawMoneySuccess: true
        })
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        wx.switchTab({
          url: '../user/index'
        })
      } else if (data.errCode == codeConfig.errCode.USER_WITHDRAW_COUNT_ERROR) {
        that.setData({
          showErrorText: true,
          errText: '每月最多可提现两次，下月再来提现吧～'
        })
        that.$wuxLoading.hide()
      } else {
        that.$wuxLoading.hide()
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }
    }).catch((res) => {
      that.$wuxLoading.hide()
      that.setData({
        pickLoading: false
      })
    })
  },

  // 账户明细
  onAccountDetail (e) {
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url
    })
  }, 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.getStorage({
      key: 'moneyTelephone',
      success(res) {
        that.setData({
          "formData.telephone": res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    if (this.data.withdrawMoneySuccess === true) {
      wx.showToast({
        title: '提现申请已提交，72小时内到账',
        icon: 'none'
      })
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.data.withdrawMoneySuccess === true) {
      wx.showToast({
        title: '提现申请已提交，72小时内到账',
        icon: 'none'
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})